package com.als.healthymananger.utils;

import org.springframework.stereotype.Component;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Component
public class KeyWordsAnalysis {
    /**
     * 查询word 在keyWord 中是否匹配
     * @param keyWord 匹配的字符串
     * @param word 关键字
     * @return word 匹配返回true，否则返回false
     */
    public boolean perfectMatch(String keyWord, String word) {
        Pattern pattern = Pattern.compile("\\b"+word+"\\b");
        Matcher matcher = pattern.matcher(keyWord);
        return matcher.matches();
    }

    /**
     * 查询word字符串是否在keyWord字符串中
     * @param keyWord
     * @param word
     * @return
     */
    public boolean match(String keyWord, String word) {
        Pattern pattern = Pattern.compile(word);
        Matcher matcher = pattern.matcher(keyWord);
        return matcher.find();
    }

    /**
     * 判断keyWord是否为id、身份证号、电话号码
     * @param keyWord
     * @return
     */
    public boolean isIdOrPhone(String keyWord) {
        Pattern pattern = Pattern.compile("\\b\\d{10,11}\\b");
        Matcher matcher = pattern.matcher(keyWord);
        if(matcher.matches())
            return true;
        else {
            Pattern p = Pattern.compile("\\b\\d{18}\\b");
            Matcher m = pattern.matcher(keyWord);
            return m.matches();
        }

    }

    /**
     * 判断keyWord是否为纯数字
     * @param keyWord
     * @return
     */
    public boolean isNum(String keyWord){
        Pattern pattern = Pattern.compile("\\b\\d+\\b");
        Matcher matcher = pattern.matcher(keyWord);
        return matcher.matches();
    }
}
